package top.hmtools.mapperUnit;

import org.springframework.stereotype.Component;

import top.hmtools.base.StringTools;
import top.hmtools.beans.ColumnsInfoBean;
import top.hmtools.common.ECurdType;
import top.hmtools.common.TableNameTools;
import top.hmtools.system.SystemInfoTools;

@Component
public class getSomeByConditionMappeerUnit extends AMapperUnitBase{

	public ECurdType getCurdType() {
		return ECurdType.READ;
	}

	public String createContent(String basePackage, String methodName,
			ColumnsInfoBean... beans) {
		StringBuffer result = new StringBuffer();
        String tableAliseName = "aa";
        String tableName = beans[0].getTable_name();
        boolean useCamelName = this.gmConfiguration.isUseCamelName();

        if(StringTools.isBlank(methodName)){
            methodName = "getSomeByCondition";
        }
        
        result.append("<select id=\""+methodName+"\" resultType=\""+TableNameTools.getBeanName(basePackage, beans[0].getTable_name())+"Bean\">");
        result.append(SystemInfoTools.getOSLineSeparator());
        
        result.append("SELECT");
        result.append(SystemInfoTools.getOSLineSeparator());
        
        
        for(ColumnsInfoBean bean:beans){
        	result.append(tableAliseName+"."+bean.getColumn_name());
        	if(useCamelName){
        		result.append("  AS  "+TableNameTools.underlineToCamelByRegex(bean.getColumn_name())+",");
        	}else{
        		result.append(",");
        	}
        	result.append(SystemInfoTools.getOSLineSeparator());
        }
        int lastIndexOf = result.lastIndexOf(",");
        result = result.replace(lastIndexOf, lastIndexOf+1, "");
        
        
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append("FROM");
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append(tableName+"  AS  "+tableAliseName);
        result.append(SystemInfoTools.getOSLineSeparator());
        result.append("<WHERE>");
        result.append(SystemInfoTools.getOSLineSeparator());
        
        for(ColumnsInfoBean bean:beans){
        	String beanName = "";
        	if(useCamelName){
        		beanName = TableNameTools.underlineToCamelByRegex(bean.getColumn_name());
        	}else{
        		beanName = bean.getColumn_name();
        	}
        	result.append("<if test=\"cond."+beanName+" !=null and cond."+beanName+" != ''\">");
        	result.append(SystemInfoTools.getOSLineSeparator());
        	result.append("AND "+tableAliseName+"."+bean.getColumn_name()+"=#{cond."+beanName+"}");
        	result.append(SystemInfoTools.getOSLineSeparator());
        	result.append("</if>");
        	result.append(SystemInfoTools.getOSLineSeparator());
        	result.append("");
        	result.append("");
        }
        
        result.append("</WHERE>");
        result.append(SystemInfoTools.getOSLineSeparator());

        result.append("");
        result.append("");
        
        result.append("</select>");
		return result.toString();
	}

}